/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ManCompras.java
 *
 * Created on 18/03/2011, 10:26:28 PM
 */

package com.catosist.run.application.compras;

import com.catosist.run.application.Util.ButtonTabComponent;
import com.catosist.run.application.viaje.gasto.DialogGasto;
import com.catosist.run.business.gestorgasto.GestorGasto;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


/**
 *
 * @author igorov
 */
public class ManCompras extends javax.swing.JFrame {

    /** Creates new form ManCompras */
    public ManCompras() {
        initComponents();
    }

    public ManCompras(GestorGasto gestorGasto) {
        initComponents();
        this.gestorGasto = gestorGasto;
    }

    public JPanel getPanel(){
        return this.jPanel1;
    }

    private void agregaPestanha(JPanel panel, String titulo) {
        this.tabWork.removeAll();
        this.tabWork.add(titulo, panel);
        this.tabWork.setSelectedIndex(this.tabWork.getTabCount() - 1);
        ButtonTabComponent pestana = new ButtonTabComponent("componente", this.tabWork);
        this.tabWork.setTabComponentAt(this.tabWork.getTabCount() - 1, pestana);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        btnVerFacturas = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnGastosViajes = new javax.swing.JButton();
        tabWork = new javax.swing.JTabbedPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.catosist.run.application.desktopapplication.DesktopApplication.class).getContext().getResourceMap(ManCompras.class);
        btnVerFacturas.setText(resourceMap.getString("btnVerFacturas.text")); // NOI18N
        btnVerFacturas.setName("btnVerFacturas"); // NOI18N
        btnVerFacturas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerFacturasActionPerformed(evt);
            }
        });

        btnNuevo.setText(resourceMap.getString("btnNuevo.text")); // NOI18N
        btnNuevo.setName("btnNuevo"); // NOI18N
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnEditar.setText(resourceMap.getString("btnEditar.text")); // NOI18N
        btnEditar.setName("btnEditar"); // NOI18N
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnEliminar.setText(resourceMap.getString("btnEliminar.text")); // NOI18N
        btnEliminar.setName("btnEliminar"); // NOI18N
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnGastosViajes.setText(resourceMap.getString("btnGastosViajes.text")); // NOI18N
        btnGastosViajes.setName("btnGastosViajes"); // NOI18N
        btnGastosViajes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGastosViajesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnVerFacturas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGastosViajes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNuevo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEliminar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnVerFacturas)
                    .addComponent(btnGastosViajes)
                    .addComponent(btnNuevo)
                    .addComponent(btnEditar)
                    .addComponent(btnEliminar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        tabWork.setName("tabWork"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 907, Short.MAX_VALUE)
                    .addComponent(tabWork, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 907, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabWork, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVerFacturasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerFacturasActionPerformed
//        gestionCompras = new GestionCompras(gestorComprobanteCompra, 0);
        gestionCompras = new GestionCompras(gestorGasto, 0);
        agregaPestanha(gestionCompras.getPanel(), "Ver las compras");
}//GEN-LAST:event_btnVerFacturasActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        DialogGasto dialogGasto = new DialogGasto(this, true, 0, 0);
        dialogGasto.setLocationRelativeTo(this);
        dialogGasto.setVisible(true);
//        refresh();

//        DialogCompra dialogCompra = new DialogCompra(this, true, gestorComprobanteCompra, 0);
//        dialogCompra.setLocationRelativeTo(this);
//        dialogCompra.setVisible(true);
        this.gestionCompras.refresh();
}//GEN-LAST:event_btnNuevoActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        try {
            int i = this.gestionCompras.getSelectedId();
//            DialogCompra dialogCompra = new DialogCompra(this, true, gestorComprobanteCompra, i);
//            dialogCompra.setLocationRelativeTo(this);
//            dialogCompra.setVisible(true);
            DialogGasto dialogGasto = new DialogGasto(this, true, 0, i);
            dialogGasto.setLocationRelativeTo(this);
            dialogGasto.setVisible(true);
            this.gestionCompras.refresh();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar un comprobante existente", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_btnEditarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        try {
            int i = this.gestionCompras.getSelectedId();
            int result = JOptionPane.showConfirmDialog(this, "Realmente desea eliminar el registro?");
            if (result == JOptionPane.YES_OPTION) {
                gestorGasto.deleteById(i);
                gestionCompras.refresh();
            }
        }catch (ArrayIndexOutOfBoundsException e) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una ruta existente", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_btnEliminarActionPerformed

    private void btnGastosViajesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGastosViajesActionPerformed
        // TODO add your handling code here:
        GastosViaje gastosViaje = new GastosViaje();
        agregaPestanha(gastosViaje.getPanel(), "Gastos de los viajes");
    }//GEN-LAST:event_btnGastosViajesActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ManCompras().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGastosViajes;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnVerFacturas;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane tabWork;
    // End of variables declaration//GEN-END:variables
    private GestionCompras gestionCompras;
//    private GestorComprobanteCompra gestorComprobanteCompra;
    private GestorGasto gestorGasto;
}
